package 练习;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

class TestReflect implements test{

    private final int yin1 = 1;
    private final String yin2 = "yin2";
    private static final int yin3 =2;
    public int yin4 = 3;
    public static int yin5 =4;
    int yin6 = 5;

    public TestReflect(int yin4, int yin6) {
        this.yin4 = yin4;
        this.yin6 = yin6;
    }

    public TestReflect(int yin4) {
        this.yin4 = yin4;
    }

    public TestReflect() {
    }


    public static void main(String[] args) {
        System.out.println("主方法");
    }

    public static int t1(int a){
        System.out.println("静态方法");
        return a;
    }

    public int t2(int b){
        System.out.println("实例方法");
        return  b;
    }

    private static int t3(int a){
        System.out.println("内部静态方法");
        return a;
    }

    private static int t4(int a){
        System.out.println("内部静态方法");
        return a;
    }

    @Override
    public void itt() {
        System.out.println("实现test的方法");
    }
}


public class t12 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        TestReflect testReflect = new TestReflect();
        Class<TestReflect> t1 = TestReflect.class;
        Class<?> t2 = Class.forName("练习.TestReflect");
        Class<? extends TestReflect> t3 = testReflect.getClass();

        TestReflect reflect = t1.newInstance();
        Field[] fields = t1.getFields();
        Field field = t1.getDeclaredField("yin1");
        int modifiers = field.getModifiers();
        System.out.println(Modifier.toString(modifiers));
        field.setAccessible(true);
        System.out.println(field.get(reflect));
        field.set(reflect,11);
        System.out.println(field.get(reflect));
        System.out.println(Arrays.toString(fields));
        Method[] methods = t2.getMethods();
        TestReflect tt = (TestReflect) t2.newInstance();
        for(Method method:methods){
            System.out.println(method.getName());
        }
        Method main = t2.getDeclaredMethod("t4",int.class);
        main.setAccessible(true);
        main.invoke(tt,1);
    }

}
